package com.itheima.auth;

import com.itheima.pojo.UserInfo;
import com.itheima.support.SysConsts;
import com.itheima.utils.RedisUtil;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Collections;

@Component
public class MyUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserInfo userInfo = RedisUtil.get(SysConsts.USER_INFO_PREX_KEY+s);
        if (userInfo!=null){
            return new User(s,userInfo.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(userInfo.getAuthority()));
        }
        return new User(s,"", Collections.emptyList());
    }
}
